import request from '@/utils/request'

export const templateFieldConfigPostRequire = (server, params) => {
    let thisParams = Object.assign({},params);
    if (thisParams.placeTypeArr) {
        thisParams.placeType = params.placeTypeArr.toString()
        thisParams.placeTypeArr = undefined
    }
    if (thisParams.allowFieldTypeArr) {
        thisParams.allowFieldType = params.allowFieldTypeArr.toString()
        thisParams.allowFieldTypeArr = undefined
    }
    if (thisParams.banFieldTypeArr) {
        thisParams.banFieldType = params.banFieldTypeArr.toString()
        thisParams.banFieldTypeArr = undefined
    }

    return new Promise((resolve) => {
        request({
            url: 'api/templateFieldConfig/' + server,
            method: 'post',
            data: thisParams
        }).then((response) => {
            if ("listPage" === server && response.data && response.data.records) {
                let arr = response.data.records || []
                arr.forEach(a => {
                    if (a.placeType && a.placeType.length > 0){
                        a.placeTypeArr = a.placeType.split(",")
                    }
                    if (a.allowFieldType && a.allowFieldType.length > 0){
                        a.allowFieldTypeArr = a.allowFieldType.split(",")
                    }
                    if (a.banFieldType && a.banFieldType.length > 0){
                        a.banFieldTypeArr = a.banFieldType.split(",")
                    }

                })
            }
            resolve(response)
        })
    })
}

export const templateFieldConfigDownload = (server, params, responseType) => {
    let thisParams = Object.assign({},params);
    if (thisParams.placeTypeArr) {
        thisParams.placeType = params.placeTypeArr.toString()
        thisParams.placeTypeArr = undefined
    }
    if (thisParams.allowFieldTypeArr) {
        thisParams.allowFieldType = params.allowFieldTypeArr.toString()
        thisParams.allowFieldTypeArr = undefined
    }
    if (thisParams.banFieldTypeArr) {
        thisParams.banFieldType = params.banFieldTypeArr.toString()
        thisParams.banFieldTypeArr = undefined
    }

  return request({
    url: 'api/templateFieldConfig/' + server,
    method: 'post',
    data: thisParams,
    responseType: responseType || "blob"
  })
}


export const templateFieldConfigPostRequireByConfig = (server, params, config) => {
    let headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    return request({
        url: 'api/templateFieldConfig/' + server,
        method: 'post',
        data: params,
        headers: Object.assign(headers, config)
    })
}
